<?php
session_start();
isLogin();

if(isset($_POST["Submits"])){
	checkNotNull(array('p_name','p_pic','p_link','p_price'),"?".$_SERVER['QUERY_STRING'],"商品");
	if($_REQUEST['act']=='modify'){
		$sql="update ".$prefix."product set p_name='".$_POST['p_name']."',p_pic='".$_POST['p_pic']."',p_link='".$_POST['p_link']."',p_price=".$_POST['p_price'].",p_type='".$_POST['p_type']."' where p_id=".$_REQUEST['p_id'];
		$pre="修改";
	}else{
		$sql="insert into ".$prefix."product (p_name,p_pic,p_price,p_type,p_link) values('".$_POST['p_name']."','".$_POST['p_pic']."',".$_POST['p_price'].",'".$_POST['p_type']."','".$_POST['p_link']."')";
		$pre="添加";
	}
	if(mysql_query($sql)){
		reload_js($pre."成功","index.php?cmd=product");
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}

$arr="";
if($_REQUEST['act']=='modify'){
	$arr=get_list_by_id('product','p_id',$_REQUEST['p_id']);
	if(!$arr){
		reload_js("错误的参数p_id","?cmd=product");
	}
}else if($_REQUEST['act']=='del'){
	$sql="delete from ".$prefix."product where p_id=".$_GET['p_id'];
	$pre="删除";
	if(mysql_query($sql)){
		reload_js($pre."成功",$_SERVER['HTTP_REFERER']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}
?>
<script language="javascript" src="../templates/<?php echo $moban;?>/js/common.js"></script>
<script language="javascript">
changeWebTitle('首页商品管理');
</script>

<div class="ContentRight">
    <div class="ContentRightTop">
      <div class="BarLeft"></div>
      <div class="ContentRightTopText">首页商品管理
	  <?php 
	  if($_GET['act']==''){
	  	echo '<a href="?cmd='.$_REQUEST['cmd'].'&amp;act=add">（点此添加商品）</a>';
	  }else{
	  	echo "<a href='".$_SERVER['HTTP_REFERER']."'>（返回）</a>";
	  }
	  ?>
	  </div>
      <div class="BarRight"></div>
      <div class="Cal"></div>
    </div>
  <div class="ContentRightBox">
	<?php if($_REQUEST['act']==''){?>
<table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td height="25" colspan="5" align="center">类别：
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=&page=".$_GET['paeg']?>">全部&nbsp;</a> 
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=女士&page=".$_GET['paeg']?>">女士(24)&nbsp;</a> 
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=男士&page=".$_GET['paeg']?>">男士(8)&nbsp;</a>
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=数码&page=".$_GET['paeg']?>">数码(4)&nbsp;</a>
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=母婴&page=".$_GET['paeg']?>">母婴(4)&nbsp;</a>
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=居家&page=".$_GET['paeg']?>">居家(4)&nbsp;</a>
		  <a href="<?php echo "?cmd=".$_GET['cmd']."&p_type=零食&page=".$_GET['paeg']?>">零食(4)&nbsp;</a>
		  </td>
        </tr>
        <tr>
          <td height="25" colspan="5" align="center">上面的数字表示首页可显示该类别商品的个数，请不时对下面商品进行整理，以减小数据库的压力</td>
        </tr>
        <tr>
          <td width="11%" height="25" align="center">ID</td>
          <td width="51%" align="center">商品名称</td>
          <td width="11%" align="center">价格 (元)</td>
          <td width="11%" align="center">类别</td>
          <td width="16%" align="center">操作</td>
        </tr>
		<?php 
		$page=$_REQUEST['page']?($_REQUEST['page']==0?1:$_REQUEST['page']):1;
		$pagesize=10;
		if($_GET['p_type']!=''){
			$sqladd="where p_type='".$_GET['p_type']."' ";
		}
		$sql="select * from ".$prefix."product ".$sqladd." limit ".($page-1)*$pagesize.",".$pagesize;
		//halt_js($sql);
		$result=mysql_query($sql);
		while($arr=mysql_fetch_array($result)){
		?>
		<tr>
          <td width="11%" height="25" align="center"><?php echo $arr['p_id']?></td>
          <td width="51%" align="center">
		  	<a href="<?php echo $arr['p_link']?>" target="_blank"><?php echo mysubstr($arr['p_name'],60,'..');?></a>		  </td>
          <td align="center"><?php echo $arr['p_price']?></td>
          <td width="11%" align="center"><?php echo $arr['p_type']?></td>
          <td width="16%" align="center"><a href="<?php echo "?cmd=".$_REQUEST['cmd']."&p_id=".$arr['p_id']."&act=modify"?>">修改</a>&nbsp;<a href="<?php echo "?cmd=".$_REQUEST['cmd']."&p_id=".$arr['p_id']."&act=del"?>" onclick="return confirm('您确认删除该条记录？删除后将不可恢复！');">删除</a></td>
        </tr>
		<?php
		}
		?>
        <tr>
          <td height="25" colspan="5" align="center">
		  <?php 
		  pagefy(get_list_count('product',$sqladd),$pagesize,'');
		  echo $pagenav;
		  ?>		  </td>
        </tr>
        <tr>
          <td height="25" align="right">版权所有：</td>
          <td colspan="4"><a href="http://www.fengsh8.com" target="_blank">风尚淘客程序</a></td>
        </tr>
      </table>
    <?php }else{?>
	  <form action="" name="productform" method="post">
    <table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td width="22%" height="25" align="right">商品名称：</td>
          <td width="78%">
            <input name="p_name" type="text" id="p_name" value="<?php echo $arr['p_name'];?>" size="50"/></td>
        </tr>
        <tr>
          <td height="25" align="right">价格：</td>
          <td>
            <input name="p_price" type="text" id="p_price" size="10" value="<?php echo $arr['p_price'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">类别：</td>
          <td>
            <label>
            <select name="p_type" id="p_type">
              <option value="女士" <?php if($arr['p_type']=='女士'){echo "selected";}?>>女士</option>
              <option value="男士" <?php if($arr['p_type']=='男士'){echo "selected";}?>>男士</option>
              <option value="数码" <?php if($arr['p_type']=='数码'){echo "selected";}?>>数码</option>
			  <option value="母婴" <?php if($arr['p_type']=='母婴'){echo "selected";}?>>母婴</option>
              <option value="居家" <?php if($arr['p_type']=='居家'){echo "selected";}?>>居家</option>
              <option value="零食" <?php if($arr['p_type']=='零食'){echo "selected";}?>>零食</option>
            </select>
            </label></td>
        </tr>
        <tr>
          <td height="25" align="right">商品图片：</td>
          <td>
            <input name="p_pic" type="text" id="p_pic" size="50" value="<?php echo $arr['p_pic'];?>"/>
          图片地址</td>
        </tr>
        <tr>
          <td height="25" align="right">商品链接：</td>
          <td>
            <input name="p_link" type="text" id="p_link" size="50" value="<?php echo $arr['p_link'];?>"/>
          链接地址</td>
        </tr>
        <tr>
          <td height="25" align="right">&nbsp;</td>
          <td><input name="Submits" type="submit" id="Submits" value=" 提  交" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="Reset" type="reset" id="Reset" value=" 重 置 " />          </td>
        </tr>
      </table>
	  </form>
    <?php }?>
    </div>
    <div class="Cal"></div>
  </div>